package com.elder.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.elder.business.domain.ElderlyApartmentBeds;
import com.elder.business.domain.ElderlyInfo;
import com.elder.business.domain.NursingRecordsDTO;
import com.elder.business.mapper.ElderlyInfoMapper;
import com.elder.common.core.page.TableDataInfo;
import com.elder.common.utils.DateUtils;
import com.elder.common.utils.LongUtils;
import com.elder.common.utils.StringNewUtils;
import com.elder.common.utils.bean.BeanUtils;
import com.elder.common.utils.uuid.SnowGenerator;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elder.business.mapper.NursingRecordsMapper;
import com.elder.business.domain.NursingRecords;
import com.elder.business.service.INursingRecordsService;

/**
 * 护理记录Service业务层处理
 * 
 * @author zyk
 * @date 2023-11-26
 */
@Service
public class NursingRecordsServiceImpl implements INursingRecordsService 
{
    @Autowired
    private NursingRecordsMapper nursingRecordsMapper;
    @Autowired
    private ElderlyInfoMapper elderlyInfoMapper;

    /**
     * 查询护理记录
     * 
     * @param id 护理记录主键
     * @return 护理记录
     */
    @Override
    public JSONObject selectNursingRecordsById(Long id)
    {
        NursingRecords v = nursingRecordsMapper.selectNursingRecordsById(id);
        ElderlyInfo elderInfo = elderlyInfoMapper.getElderInfoByElderId(v.getElderId());
        JSONObject obj = new JSONObject();
        obj.put("id", StringNewUtils.long2Str(v.getId()));
        obj.put("elderId", StringNewUtils.long2Str(v.getElderId()));
        obj.put("nursingTime", DateUtils.dateStrMIAO(v.getNursingTime()));
        obj.put("nurseName", v.getNurseName());
        obj.put("nursingItem", v.getNursingItem());
        obj.put("nursingStatus", v.getNursingStatus());
        obj.put("elderName", elderInfo.getElderName());
        obj.put("notes", v.getNotes());
        obj.put("nurseId", StringNewUtils.formatStr(v.getNurseId()));
        return obj;
    }

    /**
     * 查询护理记录列表
     *
     * @return 护理记录
     */
    @Override
    public TableDataInfo selectNursingRecordsList(String elderName, Integer pageNum, Integer pageSize)
    {
        List<Long> elderIds = null;
        if(!StringNewUtils.isEmpty(elderName)){
            List<ElderlyInfo> elderinfoList = elderlyInfoMapper.getElderinfoList(elderName);
            if(CollectionUtils.isNotEmpty(elderinfoList)){
                elderIds = elderinfoList.stream().map(ElderlyInfo::getElderId).collect(Collectors.toList());
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        List<NursingRecords> list = nursingRecordsMapper.selectNursingRecordsList(elderIds);
        PageInfo<NursingRecords> pageInfo = new PageInfo<>(list);
        List<NursingRecords> nursingRecordsList = pageInfo.getList();
        if(CollectionUtils.isEmpty(nursingRecordsList)){
            return new TableDataInfo().getDataTable(new ArrayList<>());
        }
        List<ElderlyInfo> elderlyInfoList = elderlyInfoMapper.getElderInfoByElderIds(nursingRecordsList.stream().map(NursingRecords::getElderId).distinct().collect(Collectors.toList()));
        Map<Long, String> elderMap = elderlyInfoList.stream().collect(Collectors.toMap(ElderlyInfo::getElderId, ElderlyInfo::getElderName));

        TableDataInfo data = new TableDataInfo();
        data.setTotal(pageInfo.getTotal());
        data.setRows(
            nursingRecordsList.stream().map(v->{
                JSONObject obj = new JSONObject();
                obj.put("id", StringNewUtils.long2Str(v.getId()));
                obj.put("elderId", StringNewUtils.long2Str(v.getElderId()));
                obj.put("nursingTime", DateUtils.dateStrMIAO(v.getNursingTime()));
                obj.put("nurseName", v.getNurseName());
                obj.put("nursingItem", v.getNursingItem());
                obj.put("nursingStatus", v.getNursingStatus());
                obj.put("elderName", elderMap.get(v.getElderId()));
                obj.put("notes", v.getNotes());
                obj.put("nurseId", StringNewUtils.formatStr(v.getNurseId()));
                return obj;
            }).collect(Collectors.toList()));
        return data;
    }

    /**
     * 新增护理记录
     * 
     * @param nursingDTO 护理记录
     * @return 结果
     */
    @Override
    public int insertNursingRecords(NursingRecordsDTO nursingDTO)
    {
        NursingRecords nursingRecords = new NursingRecords();
        BeanUtils.copyProperties(nursingDTO, nursingRecords);
        nursingRecords.setId(SnowGenerator.getId());
        nursingRecords.setElderId(LongUtils.str2Long(nursingDTO.getElderId()));
        nursingRecords.setNursingTime(DateUtils.getDateSeconds(nursingDTO.getNursingTime()));
        nursingRecords.setNurseId(LongUtils.str2Long(nursingDTO.getNurseId()));
        nursingRecords.setCreateTime(DateUtils.getLongTime());
        return nursingRecordsMapper.insertNursingRecords(nursingRecords);
    }

    /**
     * 修改护理记录
     * 
     * @param nursingDTO 护理记录
     * @return 结果
     */
    @Override
    public int updateNursingRecords(NursingRecordsDTO nursingDTO)
    {
        NursingRecords nursingRecords = new NursingRecords();
        BeanUtils.copyProperties(nursingDTO, nursingRecords);
        nursingRecords.setId(LongUtils.str2Long(nursingDTO.getId()));
        nursingRecords.setElderId(LongUtils.str2Long(nursingDTO.getElderId()));
        nursingRecords.setNursingTime(DateUtils.getDateSeconds(nursingDTO.getNursingTime()));
        nursingRecords.setNurseId(LongUtils.str2Long(nursingDTO.getNurseId()));
        nursingRecords.setUpdateTime(DateUtils.getLongTime());
        return nursingRecordsMapper.updateNursingRecords(nursingRecords);
    }

    /**
     * 批量删除护理记录
     * 
     * @param ids 需要删除的护理记录主键
     * @return 结果
     */
    @Override
    public int deleteNursingRecordsByIds(Long[] ids)
    {
        return nursingRecordsMapper.deleteNursingRecordsByIds(ids);
    }

    /**
     * 删除护理记录信息
     * 
     * @param id 护理记录主键
     * @return 结果
     */
    @Override
    public int deleteNursingRecordsById(Long id)
    {
        return nursingRecordsMapper.deleteNursingRecordsById(id);
    }
}
